<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>vue 案例：子传父的计数器</title>
</head>

<body>
    <div id="app">
        <h2>计数：{{total}}</h2>
        <!-- 当子组件向父组件传递的事件目标一致时，可以合并成一个事件 -->
        单独事件：<v-content @increment='changeTotal' @decrement='changeTotal'> </v-content><br>

        合并事件：<v-content2 @emitclick='changeTotal'> </v-content2>
        <h4>当子组件向父组件传递的事件目标一致时，可以合并成一个事件</h4>
    </div>

    <template id='content'>
        <div>
            <button @click="add">+1</button>
            <button @click="sub">-1</button>
        </div>
    </template>


    <script src="js/vue.js"></script>
    <script>
        const content = {
            template: '#content',
            data() {
                return {
                    counter: 0
                };
            },
            methods: {
                add() {
                    this.counter++;
                    this.$emit('increment', this.counter);
                },
                sub() {
                    this.counter--;
                    this.$emit('decrement', this.counter);
                },
            }
        }

        const content2 = {
            template: '#content',
            data() {
                return {
                    counter: 0
                };
            },
            methods: {
                add() {
                    this.counter++;
                    this.$emit('emitclick', this.counter);
                },
                sub() {
                    this.counter--;
                    this.$emit('emitclick', this.counter);
                },
            }
        }

        const app = new Vue({
            el: '#app',
            data: {
                total: 0
            },
            components: {
                'vContent': content,
                'vContent2': content2
            },
            methods: {
                changeTotal(item) {
                    this.total = item;
                    console.log(this.total);
                }
            },
        });
    </script>
</body>

</html>